package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.Comment;
import com.dachuang.canjiren.entity.User;
import com.dachuang.canjiren.entity.VO.CommentVO;
import com.dachuang.canjiren.entity.Volunteer;
import com.dachuang.canjiren.mapper.CommentMapper;
import com.dachuang.canjiren.mapper.UserMapper;
import com.dachuang.canjiren.mapper.VolunteerMapper;
import com.dachuang.canjiren.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VolunteerMapper volunteerMapper;

    @Override
    public R insertComment(String comments, Integer mainId, Integer userId, Integer volunteerId) {

        if (Objects.isNull(userId)){
            return R.error(BizCodeEnume.ERROR20006);
        }
        if ( Objects.isNull(volunteerId)) {
            return R.error(BizCodeEnume.VAILD_EXCEPTION);
        }

        if (StringUtils.isEmpty(comments)){
            return R.error(BizCodeEnume.ERROR80005);
        }
        //验证userId
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)){
            return R.error(BizCodeEnume.ERROR20004);
        }
        //验证volunteerId
        Volunteer volunteer = volunteerMapper.selectInfo(volunteerId);
        if (Objects.isNull(volunteer)){
            return R.error(BizCodeEnume.ERROR40001);
        }

        //插入到表
        Comment comment = new Comment();
        comment.setComments(comments);
        comment.setMainId(mainId);
        comment.setUserId(userId);
        comment.setVolunteerId(volunteerId);
        if (commentMapper.insertComment(comment) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectCommentList(Integer volunteerId) {
        if (Objects.isNull(volunteerId)){
            return R.error(BizCodeEnume.VAILD_EXCEPTION);
        }
        List<CommentVO> commentVOS = commentMapper.selectCommentList(volunteerId);
        R r = R.ok();
        r.put("data",commentVOS);
        return r;
    }
}
